<template>
  <el-dialog title="选择项目"
             :visible.sync="showFlag"
             :modal="false"
             width="80%"
             :show-close="false"
             :close-on-click-modal="false"
             :fullscreen="device ==='mobile'"
  >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="财年" prop="year">
        <el-date-picker v-model="queryParams.year" type="year" value-format="yyyy" placeholder="请选择财年" size="small">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-select v-model="queryParams.province" size="small" clearable filterable placeholder="请选择省份"
                   style="width: 100%;">
          <el-option v-for="item in provinceList" :key="item.code" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目编号" prop="projectNo">
        <el-input v-model.trim="queryParams.projectNo" clearable size="small" :max="50"
                  placeholder="请输入项目编号"></el-input>
      </el-form-item>
      <el-form-item label="项目名称" prop="name">
        <el-input v-model.trim="queryParams.name" clearable size="small" :max="50" placeholder="请输入项目名称"></el-input>
      </el-form-item>
      <el-form-item label="客户/代理商" prop="type">
        <el-select v-model="queryParams.type" size="small" clearable filterable placeholder="请选择客户/代理商"
                   style="width: 100%;">
          <el-option v-for="item in projectType" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户" prop="clientName">
        <el-input v-model.trim="queryParams.clientName" clearable size="small" :max="50" placeholder="请输入客户"></el-input>
      </el-form-item>
      <el-form-item label="代理商" prop="agentName">
        <el-input v-model.trim="queryParams.agentName" clearable size="small" :max="50" placeholder="请输入代理商"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input v-model.trim="queryParams.contactName" clearable size="small" :max="15"
                  placeholder="请输入联系人"></el-input>
      </el-form-item>
      <el-form-item label="进度" prop="schedule">
        <el-select v-model="queryParams.schedule" size="small" clearable filterable placeholder="请选择进度"
                   style="width: 100%;">
          <el-option v-for="item in dict.type.sys_project_schedule" :key="+item.value" :label="item.label"
                     :value="+item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="进度月份" prop="month">
        <el-date-picker v-model="queryParams.month" type="month" placeholder="请选择进度月份" value-format="yyyy-MM"
                        size="small">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属公司" prop="belongCompany">
        <el-select v-model="queryParams.belongCompany" filterable clearable default-first-option placeholder="请选择所属公司"
                   style="width: 100%">
          <el-option v-for="item in belongCompany" :key="item.id" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="itemList" max-height="500" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center" label="选择" >
        <template v-slot="scope">
          <el-radio v-model="selectedItemId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column type="index" width="50" align="center" label="序号">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column property="projectNo" label="项目编号" align="center" width="140" />
      <el-table-column property="year" label="财年" align="center" width="120" />
      <el-table-column property="name" label="项目名称" align="center" min-width="180" />
      <el-table-column property="belongCompany" label="所属公司" align="center" width="160" />
      <el-table-column property="amount" label="预算金额(万元)" align="center" width="120" />
      <el-table-column property="schedule" label="进度" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ getListValue(scope.row.schedule, dict.type.sys_project_schedule) }}</span>
        </template>
      </el-table-column>
      <el-table-column property="nowDate" label="进度日期" align="center" width="100" />
      <el-table-column property="deliveryDate" label="交付日期" align="center" width="100" />
      <el-table-column property="guaranteeDate" label="质保日期" align="center" width="100" />
      <el-table-column property="province" label="省份" align="center" width="100" />
      <el-table-column label="客户/代理商" align="center" min-width="160">
        <template slot-scope="scope">
          <span v-if="scope.row.clientName && scope.row.clientName.length > 0">
            {{ scope.row.clientName }}
          </span>
          <span v-else>{{ scope.row.agentName }}</span>
        </template>
      </el-table-column>
      <el-table-column property="facultyName" label="二级部门" align="center" width="100" />
      <el-table-column property="contactName" label="联系人" align="center" width="80" />
      <el-table-column property="contactJob" label="职务" align="center" width="80" />
      <el-table-column property="contactPhone" label="手机号码" align="center" width="120" />
      <el-table-column property="decisionName" label="决策人" align="center" width="80" />
      <el-table-column property="decisionJob" label="职务" align="center" width="80" />
      <el-table-column property="decisionPhone" label="手机号码" align="center" width="120" />
      <el-table-column property="bidAmount" label="中标金额(万元)" align="center" width="120" />
      <el-table-column property="invoiceAmount" label="累计开票金额(万元)" align="center" width="140" />
      <el-table-column property="unInvoiceAmount" label="未开票金额(万元)" align="center" width="120" />
      <el-table-column property="remark" label="备注" align="center" />
      <el-table-column property="createBy" label="创建人" align="center" width="80" />
      <el-table-column property="createTime" label="创建时间" align="center" width="160" sortable />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="handleCloseDialog">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDataListApi } from "@/api/crm/md/project";
import { getCompanyListApi } from "@/api/crm/md/customer";
import ProvinceData from '@/views/crm/data/province.json'
import { mapGetters } from 'vuex'

export default {
  name: "ProjectSelect",
  dicts: ['sys_project_schedule'],
  props: {
    showFlag: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 选中数组
      selectedItemId: null,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      // 部门树选项
      itemTypeOptions: undefined,
      // 部门名称
      itemTypeName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 省份
      provinceList: [],
      // 项目来源类型
      projectType: [
        {
          value: "客户",
          label: "客户"
        },
        {
          value: "代理商",
          label: "代理商"
        }
      ],
      belongCompany: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        belongCompany: "", // 所属公司
        month: "",
        type: "", // 项目来源的类型
        projectNo: "", // 项目编号
        year: "", // 财年
        name: "", // 项目名称 *
        startDate: "", // 开始时间
        endDate: "", // 开始时间
        contactName: "", // 联系人
        clientName: "", // 客户名称
        agentName: "", // 代理商名称
        schedule: null, // 项目进度 *
      },
    };
  },
  computed: {
    ...mapGetters([
      'device'
    ]),
  },
  watch: {
    // 根据名称筛选分类树
    itemTypeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.provinceList = ProvinceData
    this.getList();
    this.getCompanyList()
  },
  methods: {
    getCompanyList() {
      getCompanyListApi().then((res) => {
        this.belongCompany = (res.rows ?? [])
      })
    },
    // 关闭弹窗
    handleCloseDialog() {
      this.$emit('update:showFlag', false);
    },
    // 获取根据list中的value返回label
    getListValue(data, list, value = 'value') {
      let label = ''
      list.map((item) => {
        if (+item[value] === +data) {
          label = item.label
        }
      })
      return label
    },
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      getDataListApi(this.queryParams).then(response => {
          this.itemList = response.data?.list?.rows ?? [];
          this.total = response.data?.list?.total ?? 0;
          this.loading = false;
        }
      );
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
      if(row){
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.handleCloseDialog()
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      // debugger;
      if(row){
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect(){
      if(this.selectedItemId ==null || this.selectedItemId==0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRows);
      this.handleCloseDialog()
    }
  }
};
</script>
